package com.ahwei.controller;

import com.ahwei.pojo.Collect;
import com.ahwei.pojo.User;
import com.ahwei.pojo.Video;
import com.ahwei.pojo.VideoDetail;
import com.ahwei.service.CollectService;
import com.ahwei.service.impl.CollectServiceImpl;
import com.ahwei.service.impl.VideoServiceImpl;
import com.ahwei.util.anno.Controller;
import com.ahwei.util.anno.FunctionMapping;
import com.ahwei.util.anno.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author：ahwei163@qq.com
 *
 * @date: 202*-03-15 15:37:49
 * @description: 这是一个Java文件！
 */
@Controller
public class CollectController {
    @FunctionMapping("收藏视频")
    @RequestMapping("/collectVideo")
    public String collectVideo(HttpSession session){
        long vid = (long)session.getAttribute("vid");
        int result = collectService().addCollect(new Collect(User(session).getUid(),vid,new Date(),null));

        Video video = new VideoServiceImpl().findByVid(vid,User(session).getUid());
        if (video != null){
            VideoDetail videoDetail = video.getVideoDetail();
            //视频收藏量加一
            videoDetail.setCollectCount(videoDetail.getCollectCount() + 1);
            new VideoServiceImpl().updateVideoDetail(videoDetail);
        }

        return result > 0 ? "ajax:true":"ajax:false";
    }

    @FunctionMapping("取消收藏视频")
    @RequestMapping("/cancelCollectVideo")
    public String cancelCollectVideo(HttpSession session){
        long vid = (long)session.getAttribute("vid");
        int result = collectService().deleteCollect(vid,User(session).getUid());

        Video video = new VideoServiceImpl().findByVid(vid,User(session).getUid());
        if (video != null){
            VideoDetail videoDetail = video.getVideoDetail();
            //视频下载量加一
            videoDetail.setCollectCount(videoDetail.getCollectCount() - 1);
            new VideoServiceImpl().updateVideoDetail(videoDetail);
        }

        return result > 0 ? "ajax:true":"ajax:false";
    }

    @FunctionMapping("判断是否收藏该视频")
    @RequestMapping("/isCollectVideo")
    public String isCollectVideo(HttpSession session){
        long vid = (long)session.getAttribute("vid");
        List<Collect> collectList = getCollectList(session);
        for (Collect collect : collectList) {
            if (collect.getVid() == vid){
                return "ajax:true";
            }
        }
        return "ajax:false";
    }

    @FunctionMapping("获取收藏视频列表")
    @RequestMapping("/getCollectList")
    public List<Collect> getCollectList(HttpSession session){
        return collectService().findCollectByUid(User(session).getUid());
    }


    private User User(HttpSession session){
        return (User)session.getAttribute("currentUser");
    }
    private CollectService collectService(){
        return new CollectServiceImpl();
    }
}
